//jklhlo
package reportchart.object;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import reportchart.form.PieChartForm;

final public class ReadData {

    //public double avgcountuser;
    static BufferedReader input;
    // list number of user
    public List<String[]> listUser = new ArrayList<String[]>();
    static List<String> listMoney = new ArrayList<String>();
    //list dien thoai 
    static List<String> listModel = new ArrayList<String>();
    // list version Iwin
    static List<String> listVersion = new ArrayList<String>();
    static List<String> listTime = new ArrayList<String>();
    static int count = 0;
    static InputStreamReader isr;
    // data pie chart
    public static Map<String, Integer> dataVersion = new TreeMap<String, Integer>();
    //data model dien thoai
    public static Map<String, Integer> dataModel = new TreeMap<String, Integer>();
    //data time
    public static Map<String, Integer> dataTime = new TreeMap<String, Integer>();
    //data money
    public static Map<String, Integer> dataMoney = new TreeMap<String, Integer>();
    //list file .log to read
    private List<String> listFile = new ArrayList<String>();
    //key thoi gian
    public static final String N1 = "1";
    public static final String N2 = "1-5";
    public static final String N3 = "5-10";
    public static final String N4 = "10-15";
    public static final String N5 = "15-20";
    public static final String N6 = "20-25";
    public static final String N7 = "25-30";
    public static final String N8 = "30-35";
    public static final String N9 = "35-40";
    public static final String N10 = "40-45";
    public static final String N11 = "45-50";
    public static final String N12 = "50-55";
    public static final String N13 = "55-60";
    public static final String N14 = "60";
    //key money
    public static final String M1 = "win < 1000";
    public static final String M2 = "lose < 1000";
    public static final String M3 = "1000< win < 2000";
    public static final String M4 = "1000< lose < 2000";
    public static final String M5 = "2000< win < 4000";
    public static final String M6 = "2000<lose < 4000";
    public static final String M7 = "4000<win < 6000";
    public static final String M8 = "4000<lose < 6000";
    public static final String M9 = "6000<win < 8000";
    public static final String M10 = "6000<lose < 8000";
    public static final String M11 = "8000<win < 10000";
    public static final String M12 = "8000<lose < 10000";
    public static final String M13 = "win < 10000";
    public static final String M14 = "lose < 1000";
    public static final String M15 = "0";

    // contructor
    public ReadData() {
    }

    /**
     * @return the listFile
     */
    public List<String> getListFile() {
        return listFile;
    }

    /**
     * @param listFile the listFile to set
     */
    public void addListFile(String file) {
        this.listFile.add(file);
    }
//                    }
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

    /**
     * 
     * @param server: thu muc server
     * @param type : 0= version; 1=session
     * @param flag: tung ngay
     */
    public void read(String server, int type, int flag) {
        //doc file trong thu muc
        String currentDir = System.getProperty("user.dir");
        System.out.println("Current folder " + currentDir);
        String nameFile = "";
        String path = currentDir + File.separator + "log" + File.separator + server + File.separator;
        try {
            if (type == 0) {
                path = path + "version";
            } else if (type == 1) {
                path = path + "session";
            }
            File dirIn = new File(path);
            File[] dirContent = dirIn.listFiles();
            if (dirContent != null) {
                File file = dirContent[flag];
                nameFile = file.getName();
                if (file != null) {
                    isr = new InputStreamReader(new FileInputStream(file));
                    input = new BufferedReader(isr);
                    String read = null;
                    while ((read = input.readLine()) != null) {

                        listUser.add(read.split("\t"));
                        count++;
                    }
                    isr.close();
                    input.close();
                    int j = 0;
                    while (j < listUser.size()) {
                        String[] str = listUser.get(j);
                        if (type == 0) {
                            if (str.length <= 2) {
                                j++;
                            } else {
                                listVersion.add(str[1]);

                                j++;
                            }

                        } else if (type == 1) {
                            if (str.length <= 1) {
                                j++;
                            } else {
                                String[] token = str[1].split(" ");
                                listVersion.add(token[1]);
                                listMoney.add(token[2]);
                                listMoney.add(token[3]);
                                listTime.add(token[4]);
                                j++;
                            }
                        }
                    }

                }
                //lay data money
                for (int v = 0; v < listMoney.size() - 1; v++) {
                    String result = keyMoney(listMoney.get(v), listMoney.get(v + 1));
                    Integer freg = dataMoney.get(result);
                    dataMoney.put(result, (freg == null) ? 1 : freg + 1);
                }
                //lay data time
                for (int v = 0; v < listTime.size(); v++) {
                    String result = keyTime(listTime.get(v));
                    Integer freg = dataTime.get(result);
                    dataTime.put(result, (freg == null) ? 1 : freg + 1);
                }
                //lay data version
                for (String str : listVersion) {
                    Integer freq = dataVersion.get(str);
                    dataVersion.put(str, (freq == null) ? 1 : freq + 1);
                }
                PieChart.getInstance().setNameFile(nameFile);
                PieChart.getInstance().setTotal(count);

                PieChartForm a = new PieChartForm(dataVersion);
                a.pack();
                a.setVisible(true);
                a.setFlag(flag);


                listUser.clear();
                listVersion.clear();
                dataVersion.clear();
                listMoney.clear();
                listTime.clear();
                dataMoney.clear();
                dataTime.clear();

                count = 0;

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     *  read tung server theo tuan
     * @param server
     * @param type 
     */
    public void readweek(String server, int type) {

        String currentDir = System.getProperty("user.dir");
        System.out.println("Current folder " + currentDir);
        String nameFile = "";
        String path = currentDir + File.separator + "log" + File.separator + server + File.separator;
        try {
            if (type == 0) {
                path = path + "version";
            } else if (type == 1) {
                path = path + "session";
            }
            File dirIn = new File(path);
            File[] dirContent = dirIn.listFiles();
            if (dirContent != null) {
                for (int k = 0; k < dirContent.length / 7; k++) {
                    for (int i = 0; i < 7 * k + 7 && i < dirContent.length; i = i + 1) {
                        File file = dirContent[i];
                        if (file != null) {
                            nameFile = file.getName();
                            isr = new InputStreamReader(new FileInputStream(file));
                            input = new BufferedReader(isr);
                            String read = null;
                            while ((read = input.readLine()) != null) {
                                listUser.add(read.split("\t"));
                                count++;
                            }
                            isr.close();
                            input.close();
                            int j = 0;
                            while (j < listUser.size()) {
                                String[] str = listUser.get(j);
                                if (type == 0) {
                                    if (str.length <= 2) {
                                        j++;
                                    } else {
                                        listVersion.add(str[1]);
                                        j++;
                                    }

                                } else if (type == 1) {
                                    if (str.length <= 1) {
                                        j++;
                                    } else {
                                        String[] token = str[1].split(" ");
                                        listVersion.add(token[1]);
                                        listMoney.add(token[2]);
                                        listMoney.add(token[3]);
                                        listTime.add(token[4]);
                                        j++;
                                    }
                                }
                            }

                        }
                        //lay data money
                        for (int v = 0; v < listMoney.size() - 1; v++) {
                            String result = keyMoney(listMoney.get(v), listMoney.get(v + 1));
                            Integer freg = dataMoney.get(result);
                            dataMoney.put(result, (freg == null) ? 1 : freg + 1);
                        }
                        //lay data time
                        for (int v = 0; v < listTime.size(); v++) {
                            String result = keyTime(listTime.get(v));
                            Integer freg = dataTime.get(result);
                            dataTime.put(result, (freg == null) ? 1 : freg + 1);
                        }
                        //lay data version
                        for (String str : listVersion) {
                            Integer freq = dataVersion.get(str);
                            dataVersion.put(str, (freq == null) ? 1 : freq + 1);
                        }
//                        PieChart.getInstance().setNameFile(nameFile);
//                        PieChart.getInstance().setTotal(count);
//
//                        PieChartForm a = new PieChartForm(dataVersion);
//                        a.pack();
//                        a.setVisible(true);
//                        a.setFlag(flag);
                        listUser.clear();
                        listVersion.clear();
                        listMoney.clear();
                        listTime.clear();

                    }
                }
                dataVersion.clear();
                dataMoney.clear();
                dataTime.clear();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void readall(String server, int type) {

        String currentDir = System.getProperty("user.dir");
        System.out.println("Current folder " + currentDir);
        String nameFile = "";
        String path = currentDir + File.separator + "log" + File.separator + server + File.separator;
        try {
            if (type == 0) {
                path = path + "version";
            } else if (type == 1) {
                path = path + "session";
            }
            File dirIn = new File(path);
            File[] dirContent = dirIn.listFiles();
            if (dirContent != null) {
                for (int i = 0; i < dirContent.length; i++) {

                    File file = dirContent[i];
                    if (file != null) {
                        nameFile = file.getName();
                        isr = new InputStreamReader(new FileInputStream(file));
                        input = new BufferedReader(isr);
                        String read = null;
                        while ((read = input.readLine()) != null) {
                            listUser.add(read.split("\t"));
                            count++;
                        }
                        isr.close();
                        input.close();
                        int j = 0;
                        while (j < listUser.size()) {
                            String[] str = listUser.get(j);
                            if (type == 0) {
                                if (str.length <= 2) {
                                    j++;
                                } else {
                                    listVersion.add(str[1]);
                                    j++;
                                }

                            } else if (type == 1) {
                                if (str.length <= 1) {
                                    j++;
                                } else {
                                    String[] token = str[1].split(" ");
                                    listVersion.add(token[1]);
                                    listMoney.add(token[2]);
                                    listMoney.add(token[3]);
                                    listTime.add(token[4]);
                                    j++;
                                }
                            }
                        }

                    }
                    //lay data money
                    for (int v = 0; v < listMoney.size() - 1; v++) {
                        String result = keyMoney(listMoney.get(v), listMoney.get(v + 1));
                        Integer freg = dataMoney.get(result);
                        dataMoney.put(result, (freg == null) ? 1 : freg + 1);
                    }
                    //lay data time
                    for (int v = 0; v < listTime.size(); v++) {
                        String result = keyTime(listTime.get(v));
                        Integer freg = dataTime.get(result);
                        dataTime.put(result, (freg == null) ? 1 : freg + 1);
                    }
                    //lay data version
                    for (String str : listVersion) {
                        Integer freq = dataVersion.get(str);
                        dataVersion.put(str, (freq == null) ? 1 : freq + 1);
                    }
//                        PieChart.getInstance().setNameFile(nameFile);
//                        PieChart.getInstance().setTotal(count);
//
//                        PieChartForm a = new PieChartForm(dataVersion);
//                        a.pack();
//                        a.setVisible(true);
//                        a.setFlag(flag);
                    listUser.clear();
                    listVersion.clear();
                    listMoney.clear();
                    listTime.clear();
                }
            }
            dataVersion.clear();
            dataMoney.clear();
            dataTime.clear();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void readallServer(int type) {

        String currentDir = System.getProperty("user.dir");
        System.out.println("Current folder " + currentDir);
        String nameFile = "";
        String path = currentDir + File.separator + "log";
        try {
            File[] child = null;
            File log = new File(path);
            if (log.isDirectory()) {
                child = log.listFiles();
            }
            for (int k = 0; k < child.length; k++) {
                path = path + File.separator + child[k].getName() + File.separator;

                if (type == 0) {
                    path = path + "version";
                } else if (type == 1) {
                    path = path + "session";
                }
                File dirIn = new File(path);
                File[] dirContent = dirIn.listFiles();
                if (dirContent != null) {
                    for (int i = 0; i < dirContent.length; i++) {

                        File file = dirContent[i];
                        if (file != null) {
                            nameFile = file.getName();
                            isr = new InputStreamReader(new FileInputStream(file));
                            input = new BufferedReader(isr);
                            String read = null;
                            while ((read = input.readLine()) != null) {
                                listUser.add(read.split("\t"));
                                count++;
                            }
                            isr.close();
                            input.close();
                            int j = 0;
                            while (j < listUser.size()) {
                                String[] str = listUser.get(j);
                                if (type == 0) {
                                    if (str.length <= 2) {
                                        j++;
                                    } else {
                                        listVersion.add(str[1]);
                                        j++;
                                    }

                                } else if (type == 1) {
                                    if (str.length <= 1) {
                                        j++;
                                    } else {
                                        String[] token = str[1].split(" ");
                                        listVersion.add(token[1]);
                                        listMoney.add(token[2]);
                                        listMoney.add(token[3]);
                                        listTime.add(token[4]);
                                        j++;
                                    }
                                }
                            }

                        }
                        //lay data money

                        for (int v = 0; v < listMoney.size() - 1; v++) {
                            String result = keyMoney(listMoney.get(v), listMoney.get(v + 1));
                            Integer freg = dataMoney.get(result);
                            dataMoney.put(result, (freg == null) ? 1 : freg + 1);
                        }

                        //lay data time

                        for (int v = 0; v < listTime.size(); v++) {
                            String result = keyTime(listTime.get(v));
                            Integer freg = dataTime.get(result);
                            dataTime.put(result, (freg == null) ? 1 : freg + 1);
                        }
                        // lay data version

                        for (String str : listVersion) {
                            Integer freq = dataVersion.get(str);
                            dataVersion.put(str, (freq == null) ? 1 : freq + 1);
                        }
//                        PieChart.getInstance().setNameFile(nameFile);
//                        PieChart.getInstance().setTotal(count);
//
//                        PieChartForm a = new PieChartForm(dataVersion);
//                        a.pack();
//                        a.setVisible(true);
//                        a.setFlag(flag);
                        listUser.clear();
                        listVersion.clear();
                        listMoney.clear();
                        listTime.clear();
                    }
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void readServer_date(int type) {

        String currentDir = System.getProperty("user.dir");
        System.out.println("Current folder " + currentDir);
        String nameFile = "";
        String path = currentDir + File.separator + "log";
        String[] path_Parent = new String[4];
        try {
            File[] child = null;
            File[] tmp = new File[4];
            File log = new File(path);
            if (log.isDirectory()) {
                child = log.listFiles();
            }
            for (int k = 0; k < child.length; k++) {
                path_Parent[k] = path + File.separator + child[k].getName() + File.separator;

                if (type == 0) {
                    path_Parent[k] = path_Parent[k] + "version";
                } else if (type == 1) {
                    path_Parent[k] = path_Parent[k] + "session";
                }
                tmp[k] = new File(path_Parent[k]);

            }

            File[] dirContent1 = tmp[0].listFiles();
            File[] dirContent2 = tmp[1].listFiles();
            File[] dirContent3 = tmp[2].listFiles();
            File[] dirContent4 = tmp[3].listFiles();
            File[] dirContent = null;

            for (int n = 0; n < dirContent4.length; n++) {
                dirContent = file_same(dirContent4[n], dirContent1, dirContent2, dirContent3);



                if (dirContent != null) {
                    for (int i = 0; i < dirContent.length; i++) {

                        File file = dirContent[i];
                        if (file != null) {
                            nameFile = file.getName();
                            isr = new InputStreamReader(new FileInputStream(file));
                            input = new BufferedReader(isr);
                            String read = null;
                            while ((read = input.readLine()) != null) {
                                listUser.add(read.split("\t"));
                                count++;
                            }
                            isr.close();
                            input.close();
                            int j = 0;
                            while (j < listUser.size()) {
                                String[] str = listUser.get(j);
                                if (type == 0) {
                                    if (str.length <= 2) {
                                        j++;
                                    } else {
                                        listVersion.add(str[1]);
                                        j++;
                                    }

                                } else if (type == 1) {
                                    if (str.length <= 1) {
                                        j++;
                                    } else {
                                        String[] token = str[1].split(" ");
                                        listVersion.add(token[1]);
                                        listMoney.add(token[2]);
                                        listMoney.add(token[3]);
                                        listTime.add(token[4]);
                                        j++;
                                    }
                                }
                            }

                        }

                        for (int v = 0; v < listMoney.size() - 1; v++) {
                            String result = keyMoney(listMoney.get(v), listMoney.get(v + 1));
                            Integer freg = dataMoney.get(result);
                            dataMoney.put(result, (freg == null) ? 1 : freg + 1);
                        }

                        for (int v = 0; v < listTime.size(); v++) {
                            String result = keyTime(listTime.get(v));
                            Integer freg = dataTime.get(result);
                            dataTime.put(result, (freg == null) ? 1 : freg + 1);
                        }
                        for (String str : listVersion) {
                            Integer freq = dataVersion.get(str);
                            dataVersion.put(str, (freq == null) ? 1 : freq + 1);
                        }
//                        PieChart.getInstance().setNameFile(nameFile);
//                        PieChart.getInstance().setTotal(count);
//
//                        PieChartForm a = new PieChartForm(dataVersion);
//                        a.pack();
//                        a.setVisible(true);
//                        a.setFlag(flag);
                        listUser.clear();
                        listVersion.clear();
                        listTime.clear();
                        listMoney.clear();
                    }
                }
                dataVersion.clear();
                dataMoney.clear();
                dataTime.clear();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public File[] file_same(File a, File[] b, File[] c, File[] d) {
        File[] result = new File[4];
        String same_name = a.getName();
        result[0] = a;
        for (int i = 0; i < b.length; i++) {
            if (b[i].getName().equals(same_name)) {
                result[1] = b[i];
                break;
            }
        }
        if (result[1] == null) {
            return null;
        } else {
            for (int i = 0; i < c.length; i++) {
                if (c[i].getName().equals(same_name)) {
                    result[2] = c[i];
                    break;
                }
            }
            if (result[2] == null) {
                return null;
            } else {
                for (int i = 0; i < d.length; i++) {
                    if (d[i].getName().equals(same_name)) {
                        result[3] = d[i];
                        break;
                    }

                }
                if (result[3] == null) {
                    return null;
                } else {
                    return result;
                }
            }
        }

    }

    public String keyTime(String time) {

        long t = Long.parseLong(time) / 1000;
        if (t < 60) {
            return N1;
        } else if (t >= 60 && t < 300) {
            return N2;
        } else if (t >= 300 && t < 600) {
            return N3;
        } else if (t >= 600 && t < 900) {
            return N4;
        } else if (t >= 900 && t < 1200) {
            return N5;
        } else if (t >= 1200 && t < 1500) {
            return N6;
        } else if (t >= 1500 && t < 1800) {
            return N7;
        } else if (t >= 1800 && t < 2100) {
            return N8;
        } else if (t >= 2100 && t < 2400) {
            return N9;
        } else if (t >= 2400 && t < 2700) {
            return N10;
        } else if (t >= 2700 && t < 3000) {
            return N11;
        } else if (t >= 3000 && t < 3300) {
            return N12;
        } else if (t >= 3300 && t < 3600) {
            return N13;
        } else if (t >= 3600) {
            return N14;
        }
        return null;
    }

    public String keyMoney(String m1, String m2) {
        int t1 = Integer.parseInt(m1);
        int t2 = Integer.parseInt(m2);
        int a = t2 - t1;
        if (a == 0) {
            return M15;
        } else if (a > 0 && a < 1000) {
            return M1;
        } else if (a < 0 && (-1) * a < 1000) {
            return M2;
        } else if (a > 0 && a >= 1000 && a < 2000) {
            return M3;
        } else if (a < 0 && (-1) * a >= 1000 && (-1) * a < 2000) {
            return M4;
        } else if (a > 0 && a >= 2000 && a < 4000) {
            return M5;
        } else if (a < 0 && (-1) * a >= 2000 && (-1) * a < 4000) {
            return M6;
        } else if (a > 0 && a >= 4000 && a < 6000) {
            return M7;
        } else if (a < 0 && (-1) * a >= 4000 && (-1) * a < 6000) {
            return M8;
        } else if (a > 0 && a >= 6000 && a < 8000) {
            return M9;
        } else if (a < 0 && (-1) * a >= 6000 && (-1) * a < 8000) {
            return M10;
        } else if (a > 0 && a >= 8000 && a < 10000) {
            return M11;
        } else if (a < 0 && (-1) * a >= 8000 && (-1) * a < 10000) {
            return M12;
        } else if (a > 0 && a > 10000) {
            return M13;
        } else if (a < 0 && (-1) * a > 1000) {
            return M14;
        }
        return null;
    }
}
